// 状态
import {
  getBaseCategoryList,
  getFloorList,
  getGoodsList,
  getLikeList,
  getProductInfo,
  getRankList,
  postSearchGoodsList,
} from '@/api/product'

const state = {
  // 三级分类数据
  categoryList: [],
  // 商品排行
  rankList: [],
  // 商品排行2
  goodsList: [],
  // 猜你喜欢的商品数据
  likeList: [],
  // 猜你喜欢的商品数据的总页数
  likePageSum: 1,
  // 楼层数据
  floorList: [],
  // 搜索出来的商品
  searchResult: {
    pageNo: 1, // 初始页码
    pageSize: 5, // 每页显示的数量
    total: 1, // 总条数
  },
  // 商品的详情
  productInfo: {
    // 面包屑导航相关数据
    categoryView: {},
    // 商品详情
    skuInfo: {},
  },
  //添加购物车信息成功
  cartInfoSuccess: {},
}
const mutations = {
  //更新添加购物车成功
  UP_CARTINFO_SUCCESS(state, payload) {
    state.cartInfoSuccess = payload
  },
  // 切换配置
  UP_spuSaleAttrList_CHECKED(state, { id1, id2 }) {
    const { spuSaleAttrValueList } = state.productInfo.spuSaleAttrList.find(
      (v) => v.id === id1
    )
    // 之前选中的
    spuSaleAttrValueList.find((v) => v.isChecked === '1').isChecked = '0'
    // 将选中的配件进行高亮显示
    spuSaleAttrValueList.find((v) => v.id === id2).isChecked = '1'
  },
  // 更改skuInfo->skuImageList->isDefault的值
  // UP_SKUIMAGELIST_ISDEFAULT(state, id) {
  //   // 获取到之前选中的图片
  //   const checkedInfo = state.productInfo.skuInfo.skuImageList.find(
  //     (v) => v.isDefault === '1'
  //   )
  //   // 将之前选中的图片isDefault设置为0（取消高亮）
  //   checkedInfo.isDefault = '0'
  //   // 获取当前点击的图片
  //   const info = state.productInfo.skuInfo.skuImageList.find((v) => v.id === id)
  //   // 让当前点击的图片高亮。
  //   info.isDefault = '1'
  //   //将缩略图上方图片进行更新
  //   state.productInfo.skuInfo.skuDefaultImg = info.imgUrl
  // },
  // 更新商品详情
  UP_PRODUCT_INFO(state, payload) {
    state.productInfo = payload
  },
  CHANGE_ACTIVEINDEX_BYID(state, payload) {
    // payload-->{id:条件,activeIndex:修改的值}
    const info = state.floorList.find((v) => v.id === payload.id)
    info.activeIndex = payload.activeIndex
  },
  // 更新三级分类数据
  UP_CATEGORY_LIST(state, payload) {
    state.categoryList = payload
  },
  UP_RANKLIST(state, payload) {
    state.rankList = payload
  },
  UP_GOODSLIST(state, payload) {
    state.goodsList = payload
  },
  UP_LIKELIST_AND_PAGESUM(state, payload) {
    state.likeList = payload.likeList
    state.likePageSum = payload.pageSum
  },
  UP_FLOORLIST(state, payload) {
    state.floorList = payload.map((item) => {
      item.activeIndex = 0
      return item
    })
  },
  UP_SEARCH_RESULT(state, payload) {
    state.searchResult = payload
  },
}
// action:异步行为
const actions = {
  async getProductInfoAsync({ commit }, id) {
    const res = await getProductInfo(id)
    commit('UP_PRODUCT_INFO', res.data)
  },
  // 搜索
  async postSearchGoodsListAsync({ commit }, body) {
    const res = await postSearchGoodsList(body)
    commit('UP_SEARCH_RESULT', res.data)
  },

  // 获取三级数据分类
  async getBaseCategoryListAsync({ commit, state }, limit = 15) {
    if (state.categoryList.length > 0) return
    const res = await getBaseCategoryList()
    commit('UP_CATEGORY_LIST', res.data.slice(0, limit))
  },
  // 获取商品排行
  async getRankListAsync({ commit }) {
    const res = await getRankList()
    commit('UP_RANKLIST', res.rankList)
  },
  // 获取商品排行 2
  async getGoodsListAsync({ commit }, type = 1) {
    const res = await getGoodsList(type)
    commit('UP_GOODSLIST', res.goodsList)
  },
  // 获取猜你喜欢
  async getLikeListAsync({ commit }, { pageNo, pageSize = 6 }) {
    const res = await getLikeList(pageNo, pageSize)
    commit('UP_LIKELIST_AND_PAGESUM', res)
  },
  async getFloorListAsync({ commit }) {
    const res = await getFloorList()
    commit('UP_FLOORLIST', res.floorList)
  },
}
export default {
  namespaced: true,
  state,
  actions,
  mutations,
}
